import AssemblerCustom from "../Common/Assemblers/AssemblerCustom";
import TextureSliceAssembler from "./TextureSliceAssembler";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('TextureSliceRender/TextureSliceRender')
export default class TextureSliceRender extends cc.RenderComponent {

    /** 纹理 */
    @property()
    private _texture: cc.Texture2D = null;

    @property({ type: cc.Texture2D, tooltip: '纹理' })
    public get texture(): cc.Texture2D {
        return this._texture;
    }
    public set texture(value: cc.Texture2D) {
        // if (this._texture === value) { return; }
        this._texture = value;

        this.node.width = this._texture.width;
        this.node.height = this._texture.height;

        let l = -value.width / 2, b = -value.height / 2, t = value.height / 2, r = value.width / 2;
        this.polygon = [cc.v2(l, b), cc.v2(r, b), cc.v2(r, t), cc.v2(l, t)];

        this.resetVerts();
        // this.updateMaterial();
    }

    private _polygon: cc.Vec2[] = [];

    /** 四边形四个顶点 (左下, 右下, 右上, 左上) */
    public get polygon(): cc.Vec2[] {
        if (this._polygon.length == 0 && this.texture) {
            let value = this.texture;
            let l = -value.width / 2, b = -value.height / 2, t = value.height / 2, r = value.width / 2;
            this._polygon = [cc.v2(l, b), cc.v2(r, b), cc.v2(r, t), cc.v2(l, t)];
        }
        return this._polygon;
    }

    public set polygon(value: cc.Vec2[]) {
        this._polygon = value;
        this.resetVerts();
    }

    protected _initedMaterial: boolean = false

    onEnable() {
        super.onEnable();
        this.init();

        if (!this._assembler) { return; }
        //@ts-ignore
        this._assembler.updateRenderData(this);
    }

    start() {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, (event: cc.Event.EventTouch) => {
            this.node.x += event.getDeltaX();
            this.node.y += event.getDeltaY();
        }, this);
    }

    /**
     * @dec 更新材质  
     * @dec 设置顶点数据修改 `setVertsDirty()`
     */
    public init() {
        if (!this._initedMaterial) {
            this.updateMaterial();
        }
        this.setVertsDirty();
    }

    /**
     * @dec override
     * @dec 在父类 `CCRenderCompoent` 的 `__proload` 中会被调用
     */
    public _resetAssembler() {
        let assembler = this._assembler = new TextureSliceAssembler();
        assembler.init(this);
        this._updateColor();
        this.setVertsDirty();
    }

    /**
     * @dec Update material properties
     * @dec 设置Effect(builtin-2d-sprite.effect)里面使用的纹理属性
     * @dec 或者用户自定的纹理属性
     */
    protected _updateMaterial() {
        let material = this.getMaterial(0);
        if (material) {
            material.define('CC_USE_MODEL', 1);
            material.setProperty('texture', this.texture);
        }
    }

    /**
     * @dec 更新材质
     */
    protected updateMaterial() {
        this._updateMaterial();
        this._initedMaterial = true;
    }

    private resetVerts() {
        //@ts-ignore
        // this._assembler.updatePoint(this);

        //@ts-ignore
        // this._assembler.initData();

        // this._initedMaterial = false;
        this.init();
        if (!this._assembler) { return; }
        //@ts-ignore
        this._assembler.updateRenderData(this);

        //@ts-ignore
        // this._assembler.updateColor(this, null);
    }
}